package com.qdxwx.web.controller.ajax;


import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.page.PageRequest;
import com.qdxwx.data.service.member.MemberRecordService;
import com.qdxwx.data.service.user.UserLetterService;
import com.qdxwx.data.service.user.UserService;
import com.qdxwx.data.service.user.UserTokenService;
import com.qdxwx.models.enums.LetterStatus;
import com.qdxwx.models.member.MemberRecord;
import com.qdxwx.models.user.User;
import com.qdxwx.models.user.UserLetter;
import com.qdxwx.web.controller.BaseController;
import com.qdxwx.web.utils.LoginUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.util.List;

/**
 * 个人中心  Controller
 * @author developer
 */
@Controller
@RequestMapping("/ajax/user")
public class AjaxUserController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(AjaxUserController.class);
    @Autowired
    private UserLetterService userLetterService;
    @Autowired
    private MemberRecordService memberRecordService;
    @Autowired
    private UserTokenService userTokenService;
    @Autowired
    private UserService userService;

    /**
     * 获取登录
     */
    @RequestMapping("/current/info.json")
    @ResponseBody
    public AjaxResult getLoginUser(HttpServletRequest request) {
        User user = LoginUser.getUser(request);
        if (user == null || user.getUserId() == 0) {
            user = userService.getByCookie(LoginUser.getCookie(request));
            if (user == null) {
                return AjaxResult.error("not login!");
            }
            userService.updateLoginTime(user.getUserId());
            LoginUser.setUser(request, user);
        }
        return AjaxResult.success(user);
    }

    /**
     * 获取登录
     */
    @RequestMapping("/current/vip.json")
    @ResponseBody
    public AjaxResult getVipInfo(HttpServletRequest request) {
        long userId = LoginUser.getUserId(request);
        if (userId < 1) {
            return AjaxResult.error("not login!");
        }
        /*查询所有会员类型当前时间的开通情况*/
        MemberRecord memberRecord = memberRecordService.getByUserId(userId, null);
        if (memberRecord == null || memberRecord.getEndDate() == null || memberRecord.getEndDate().isBefore(LocalDate.now())) {
            return AjaxResult.error("not vip!");
        } else {
            return AjaxResult.success(memberRecord);
        }
    }

    /**
     * 查询该用户有多少未读消息
     * RequestMapping("/ajax/queryUnReadLetter.json")
     */
    @RequestMapping("/current/letter.json")
    @ResponseBody
    public AjaxResult getUnReadLetter(HttpServletRequest request) {
        long userId = LoginUser.getUserId(request);
        if (userId < 1) {
            return AjaxResult.error("not login!");
        }
        UserLetter userLetter = new UserLetter();
        userLetter.setUserId(userId);// set用户id
        userLetter.setIsRead(0);
        List<UserLetter> userLetterList = userLetterService.getList(userLetter, new PageRequest(1, 10));// 查询站内信收件箱
        return AjaxResult.success(userLetterList);
    }

    /**
     * 退出登录
     */
    @RequestMapping("/logout.json")
    @ResponseBody
    public AjaxResult outLogin(HttpServletRequest request, HttpServletResponse response) {
        try {
            long userId = LoginUser.getUserId(request);
            if (userId > 0) {
                userTokenService.delete(userId);
            }
            LoginUser.removeUser(request.getSession());
            LoginUser.removeCookie(response);
            return AjaxResult.success();
        } catch (Exception e) {
            logger.error("outLogin-error", e);
            return AjaxResult.error();
        }
    }
}